package FuncionalOptionsTest

import (
	"bytes"
	"encoding/gob"
	"fmt"
)

type T struct {
	Age   int
	Inter Inter
}

func (m *T) Set(x int) {
	m.Age += x
}

type Inter interface {
	Hi(int)
}

type TestFunc func(int) int

func (m TestFunc) Hi(a int) {
	fmt.Println("哈哈", m(a))
}

type T2Func func(int) int

func (m T2Func) Hi(a int) {
	fmt.Println("2222:", m(a))
}

func m1(t *T) TestFunc {
	return func(a int) int {
		t.Age += a * 2
		return t.Age
	}
}

func m2(t *T) T2Func {
	return func(a int) int {
		t.Age += a * 3
		return t.Age
	}
}

func TestInter() {
	s := T{}
	f1 := m1(&s)
	f2 := m2(&s)
	s.Set(10)
	for i := 1; i < 4; i++ {
		s.Inter = f1
		if s.Age%2 == 0 {
			s.Inter = f2
		}
		s.Inter.Hi(i)
	}
	t := []byte{97, 98}
	var buf *bytes.Buffer
	buf = bytes.NewBuffer(t)
	unreadBytes := buf.Bytes()
	fmt.Printf("The unread bytes of the buffer: %v\n", unreadBytes)
}

func deepCopyNext(src, dst interface{}) (err error) {
	/*	if reflect.TypeOf(dst).Kind() != reflect.Ptr {
		return errors.New("目标对象必须是指针？")
	}*/
	var buf bytes.Buffer
	err = gob.NewEncoder(&buf).Encode(src)
	if err != nil {
		return
	}
	err = gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
	return
}
